<template>
  <!-- 弹窗组件 -->
  <el-dialog :visible.sync="dialogFormVisible" :title="title" :before-close="closeDialog" :close-on-click-modal="false" width="450px">
    <el-form
      ref="ruleForm"
      :model="form"
      :rules="rules"
      :disabled="isDisabled"
      label-width="100px"
    >
      <el-form-item :label="$t('handlerId')" prop="handlerId">
        <el-select v-model="form.handlerId" :disabled="eqptypedisable" clearable filterable >
          <el-option
            v-for="item in handlerIds"
            :key="item.id"
            :value="item.name"
            :label="item.name"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('testerId')" prop="testerId">
        <el-select v-model="form.testerId" clearable filterable >
          <el-option
            v-for="(item, index) in testIds"
            :key="index"
            :value="item.name"
            :label="item.name"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div v-if="!isDisabled" slot="footer" class="dialog-footer">
      <el-button icon="el-icon-circle-close" @click="closeDialog">{{
        $t("cancel")
      }}</el-button>
      <el-button v-btnThrottle="doSave" type="primary" icon="el-icon-receiving">{{ $t('save') }}</el-button>
    </div>
  </el-dialog>
</template>
<script>
import eqpJob from '@/api/equipment/eqpbase'
import crudJob from '@/api/equipment/eqpBinding'
import networkTips from '@/mixins/networkTips'
export default {
  name: 'MyDialog',
  mixins: [networkTips],
  props: {
    isDisabled: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      form: {},
      handlerIds: [],
      testIds: [],
      dialogFormVisible: false,
      eqptypedisable: false,
      rules: {
        handlerId: [
          { required: true, message: `${this.$t('pleaseSelect')}${this.$t('handlerId')}`, trigger: 'change' }
        ],
        testerId: [
          { required: true, message: `${this.$t('pleaseSelect')}${this.$t('testerId')}`, trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    getTesterList() {
      eqpJob.getEqpHeadList().then((res) => {
        this.testIds = res.data
      })
    },
    getHandlerList() {
      eqpJob.getTestList({ EqpType: 'HANDLER' }).then((res) => {
        this.handlerIds = res.data
      })
    },
    // 新增数据
    doSave() {
      this.$refs['ruleForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        if (this.form.id) {
          // 存在id则是更新
          crudJob.edit({ id: this.form.id, ...this.form }).then(res => {
            this.netTips(res, this.closeDialog, this.clearDialog)
          })
        } else {
          // 不存在id则是新增
          crudJob.add(this.form).then(res => {
            this.netTips(res, this.closeDialog, this.clearDialog)
          })
        }
      })
    },
    // 打开弹窗
    openDialog(row) {
      if (row && row.id) {
        crudJob.get({ id: row.id }).then(res => {
          this.form = res.data
        })
        this.eqptypedisable = true
      } else {
        this.eqptypedisable = false
      }
      this.getHandlerList()
      this.getTesterList()
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.ruleForm.clearValidate()
      })
    },
    // 关闭弹窗
    closeDialog() {
      Object.assign(this.$data, this.$options.data.call(this))
    },
    clearDialog() {
      this.$emit('initList')
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-form .el-form-item .el-form-item__content{
  width: 60%;
  .el-select {
    width: 100%;
  }
}
</style>
